package com.shigen.demo.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.shigen.demo.entity.Result;

/**
 * @author shigenfu
 * @date 2023/11/26 11:36
 */
public class MergeUtil {

    private static final CopyOptions OPTIONS = CopyOptions.create().setIgnoreNullValue(true).setOverride(false);

    private static final CopyOptions OVERRIDE_OPTIONS = CopyOptions.create().setIgnoreNullValue(true).setOverride(true);

    public static Object merge(Object sourceBean, Object targetBean) {
        BeanUtil.copyProperties(sourceBean, targetBean, OPTIONS);
        return targetBean;
    }

    public static Object mergeOverride(Object sourceBean, Object targetBean) {
        BeanUtil.copyProperties(sourceBean, targetBean, OVERRIDE_OPTIONS);
        return targetBean;
    }

    public static void main(String[] args) {
        Result<String> successResult = new Result<>(200, null, "data");
        Result<Object> errorResult = new Result<>(null, "error", "errorData");
        System.out.println(successResult + " " + errorResult);
        // BeanUtil.copyProperties(successResult, errorResult);

        // Result(status=200, msg=error, data=errorData)
        // MergeUtil.merge(successResult, errorResult);

        // Result(status=200, msg=error, data=data)
        MergeUtil.mergeOverride(successResult, errorResult);
        System.out.println(errorResult);
    }

}
